package com.amos.cmp_android.ui;

import android.support.design.widget.Snackbar;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.amos.cmp_android.R;
import com.amos.cmp_android.model.SchoolCarModel;
import com.amos.cmp_android.net.STError;
import com.amos.cmp_android.net.STNetTask;
import com.amos.cmp_android.presenter.GetSchoolCarPresenter;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tlh on 2016/4/13.
 */
public class SchoolBusActivity extends BaseActivity implements
        STNetTask.BeforeQueryHook, STNetTask.AfterQueryHook,GetSchoolCarPresenter.GetSchoolCarListener,
        SwipeRefreshLayout.OnRefreshListener,BaseRecyclerAdapter.OnItemClickListener{
    private GetSchoolCarPresenter presenter;
    private BaseRecyclerAdapter<SchoolCarModel> adapter;
    private ArrayList<SchoolCarModel> mList;
    @Override
    protected String getToolbarTitle() {
        return "校车浏览";
    }

    @Override
    protected void initData() {
        super.initData();
        mList = new ArrayList<>();
        swipeRefresh.setColorSchemeResources(R.color.blue, R.color.gray, R.color.green, R.color.red);//设置跑动的颜色值
        swipeRefresh.setOnRefreshListener(this);
        adapter=new BaseRecyclerAdapter<SchoolCarModel>(this,mList) {
            @Override
            public int getItemLayoutId(int viewType) {
                return R.layout.recycler_item_school_bus;
            }
            @Override
            public void bindData(RecyclerViewHolder holder, int position, SchoolCarModel item) {
                holder.setText(R.id.tv_busId,String.valueOf(item.getBid()))
                        .setText(R.id.tv_district,item.getBdistrict())
                        .setText(R.id.tv_start,item.getBstart())
                        .setText(R.id.tv_time,item.getBtime())
                        .setText(R.id.tv_route,item.getBroute());
            }
        };
        adapter.setOnItemClickListener(this);
        recyclerView.setAdapter(adapter);
        presenter = new GetSchoolCarPresenter(this);
        presenter.setBeforeQueryHook(this);
        presenter.setAfterQueryHook(this);
        presenter.setListener(this, mList);
        presenter.getSchoolCar(1,50);
    }

    @Override
    protected void initView() {
        super.initView();
        toolbar.setOnMenuItemClickListener(menuItemClickListener);
    }

    @Override
    public void afterQueryHook() {
        if (swipeRefresh != null && swipeRefresh.isRefreshing())
            swipeRefresh.setRefreshing(false);
    }

    @Override
    public void beforeQueryHook() {
        if (swipeRefresh != null && !swipeRefresh.isRefreshing())
            swipeRefresh.setRefreshing(true);
    }

    @Override
    public void onSuccess() {
        if (adapter != null)
            adapter.notifyDataSetChanged();
        if (swipeRefresh != null && swipeRefresh.isRefreshing())
            swipeRefresh.setRefreshing(false);
        Toast.makeText(this, "查询成功", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onFailed(STError error) {
        Toast.makeText(this, "查询失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onItemClick(View itemView, int pos) {
        SchoolCarModel model = mList.get(pos);
        Snackbar.make(itemView, "发车时间："+model.getBtime()+" 路线："+model.getBroute(), Snackbar.LENGTH_LONG)
                .show();
    }

    @Override
    public void onRefresh() {
        //重新获取数据
        presenter.getSchoolCar(1,50);
    }

    private Toolbar.OnMenuItemClickListener menuItemClickListener = new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            switch (menuItem.getItemId()) {
                case R.id.btn_search:
                    if (mList==null||mList.isEmpty()){
                        Toast.makeText(SchoolBusActivity.this, "请稍后再查询", Toast.LENGTH_SHORT).show();
                        return true;
                    }
                    //与校区列表的下标对应，每一项对应一个发车地点的列表
                    List<List<String>> startPointList=new ArrayList<>();
                    //校区列表
                    List<String> districtList=new ArrayList<>();
                    for (SchoolCarModel model : mList) {
                        String district=model.getBdistrict();
                        String startPoint=model.getBstart();
                        //如果校区未包含，把校区和发车地点添加到各自对应的列表当中
                        if(!districtList.contains(district)){
                            districtList.add(district);
                            startPointList.add(new ArrayList<String>());
                            startPointList.get(districtList.indexOf(district)).add(startPoint);
                        }else{
                            //如果校区已包含
                            List<String> list = startPointList.get(districtList.indexOf(district));
                            if (!list.contains(startPoint)){
                                list.add(startPoint);
                            }
                        }
                    }
                    Logger.d(startPointList.toString());
                    Logger.d(districtList.toString());
                    // TODO: 2016/4/13 转到校车搜索Activity，把districtList，startPointList，mList传过去
                    //...
                    SearchSchoolBusActivity.startSearchSchoolBusActivity(SchoolBusActivity.this,mList,districtList,startPointList);
                    break;
            }
            return true;
        }
    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_school_bus,menu);
        return true;
    }
}
